<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="/wesite/view/common/base.jsp" %>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>积分中心</title>
    <%@ include file="/wesite/view/common/common.jsp" %>
</head>
<style>
    .integral-center .integral-con .sign-in-msg{
        min-width:3.2rem;
        width:auto;
        background-color: #0daeff;
    }
    .integral-center .integral-con .sign-in-msg.sign-in{
        background-color: #ccc;
    }
</style>
<body>
<div class="integral-center">
    <section class="integral-con">
        <header class="title-msg">每日签到</header>
        <p class="integral-msg"></p>
        <section class="sign-in-list">
            <ul>

            </ul>

        </section>
        <article class="msg-rule">
            <header>签到规则</header>
            <section class="rulea-con"> ${integral_rule}</section>
        </article>

        <a class="sign-in-msg" href="javascript:;">签到</a>
    </section>
    <section class="coupon-list">
        <header>兑换优惠券 <span>(当前积分：<em id="integralSum"><fmt:formatNumber value="${myIntegral.integral + myIntegral.ly_integral}" type="number"></fmt:formatNumber></em>)</span> <a href="javascript:;">如何获取积分？</a></header>
        <ul>
            <c:forEach var="coupon" items="${couponList}">
                <c:choose>
                    <c:when test="${coupon.amount > coupon.sales}">
                        <li class="coupon-cur">
                            <div class="coupon-sum fl">￥<span><fmt:formatNumber value="${coupon.offer_money}" type="number"></fmt:formatNumber></span></div>
                            <div class="coupon-text fl">
                                <p class="text-co">满<fmt:formatNumber value="${coupon.min_money}" type="number"></fmt:formatNumber>元可使用</p>
                                <p class="text-time">有效期：<fmt:formatNumber value="${coupon.activity_day}" type="number"></fmt:formatNumber>天内有效</p>
                                <p class="text-int">积分：<fmt:formatNumber value="${coupon.offer_money * 100}" type="number"></fmt:formatNumber></p>
                            </div>
                            <div class="cl"></div>
                            <a class="convert-btn" onclick="exchangeCoupon(${coupon.id},${coupon.offer_money * 100})" href="javascript:;">立即兑换</a>
                        </li>
                    </c:when>
                    <c:otherwise>
                        <li>
                            <div class="coupon-sum fl">￥<span><fmt:formatNumber value="${coupon.offer_money}" type="number"></fmt:formatNumber></span></div>
                            <div class="coupon-text fl">
                                <p class="text-co">满<fmt:formatNumber value="${coupon.min_money}" type="number"></fmt:formatNumber>元可使用</p>
                                <p class="text-time">有效期：<fmt:formatNumber value="${coupon.activity_day}" type="number"></fmt:formatNumber>天内有效</p>
                                <p class="text-int">积分：<fmt:formatNumber value="${coupon.offer_money * 100}" type="number"></fmt:formatNumber></p>
                            </div>
                            <div class="cl"></div>
                            <a class="convert-btn" href="javascript:;">已兑完</a>
                        </li>
                    </c:otherwise>
                </c:choose>

            </c:forEach>

        </ul>
    </section>
</div>

<article class="light-hint" style="display: none;">优惠券兑换成功</article>
<input type="hidden" name="signcount" value="0">
<input type="hidden" name="integral_today" value="${integral_today}">
<input type="hidden" name="days" value="${days}">
<script>
    var integral_today = JSON.parse($("[name = 'integral_today']").val());
    var days = JSON.parse($("[name = 'days']").val());
    var days_integrals = ${days_integrals};
    days_integrals = eval(days_integrals);
    showSign();
    function showSign() {
        $w.httpRequest({
            url:"/we/home/signStatus",
            ok:function (ret) {
                var data =  ret.Data;
                var count = data.signcount;
                $("[name='signcount']").val(count);
                var h = "";
                for(var i = 0;i<count;i++){
                    h += "<li class='sign-item'>"
                        + "<i id='day" + (i+1) + "' class='cur'></i>"
                        + "<span>day"+(i+1)+"</span>"
                        + "</li>";
                }
                if(data.issign) {
                    h += "<li class='sign-item'>"
                        + "<i id='day" + (count+1) + "' class='cur cur-i'></i>"
                        + "<span>day" + (count+1) + "</span>"
                        + "</li>";
                    count++;
                    $(".sign-in-msg").text("已签到");
                    $(".sign-in-msg").addClass("sign-in");
                }else {
                    $(".sign-in-msg").on("click",Sign);
                }
                for(var j = count; j < 7 ; j++) {
                    h += "<li class='sign-item'>"
                        + "<i id='day" + (j+1) + "'></i>"
                        + "<span>day"+(j+1)+"</span>"
                        + "</li>";
                }

                h += ' <div class="cl"></div>';
                $(".sign-in-list > ul").html(h);
                var flag = days;
                for(var i=0;i<=flag.length;i++){
                    $("#day"+flag[i]).addClass("ico-active");
                }

                if(days.length == 1) {
                    if(count < days[0]){
                        var msg = (count == 0 ? "" : "再");
                        msg += "连续签到 " + (days[0] - count )+ "天可额外获得 +" + days_integrals[days[0]] + "积分";
                        $(".integral-msg").text(msg);
                    }
                }else if(days.length > 1){
                    if(count < days[0]){
                        var msg = (count == 0 ? "" : "再");
                        msg += "连续签到 " + (days[0] - count )+ "天可额外获得 +" + days_integrals[days[0]] + "积分";
                        $(".integral-msg").text(msg);
                    }else {
                        for(var i = 1, len = days.length ; i < len ; i++){
                            if(count < days[i]){
                                var msg = "再连续签到 " + (days[i] - count )+ "天可额外获得 +" + days_integrals[days[i]] + "积分";
                                $(".integral-msg").text(msg);
                                break;
                            }
                        }
                    }

                }
            }
        })
    };

    /**
     * 点击签到按钮
     */
    function Sign() {
        $w.httpRequest({
            url:apiroot+"/we/home/sign",
            data:{},
            ok:function(ret){
                var inteSum = ret.Data.integral + ret.Data.ly_integral;
                $("#integralSum").text(inteSum);
                var count = parseInt($("[name='signcount']").val());
                count++;
                if(days.length == 1) {
                    if(count < days[0]){
                        var msg = (count == 0 ? "" : "再");
                        msg += "连续签到 " + (days[0] - count )+ "天可额外获得 +" + days_integrals[days[0]] + "积分";
                        $(".integral-msg").text(msg);
                    }
                }else if(days.length > 1){
                    if(count < days[0]){
                        //integral = days_integrals.get(days[0]);
                        var msg = (count == 0 ? "" : "再");
                        msg += "连续签到 " + (days[0] - count )+ "天可额外获得 +" + days_integrals[days[0]] + "积分";
                        $(".integral-msg").text(msg);
                    }else {
                        for(var i = 1, len = days.length ; i < len ; i++){
                            if(count < days[i]){
                                var msg = "再连续签到 " + (days[i] - count )+ "天可额外获得 +" + days_integrals[days[i]] + "积分";
                                $(".integral-msg").text(msg);
                                break;
                            }
                        }
                    }

                }
                $(".sign-in-msg").text(("已签到+" + getIntegral(count)));
                $("#day"+count).addClass("cur cur-i");
                $(".sign-in-msg").off("click",Sign);
                $(".sign-in-msg").addClass("sign-in");
            }
        })
    }

    function getIntegral(count) {
        var integral = integral_today;

        for(var i = 0, len = days.length ; i < len ; i++){
            if(count == days[i]){
                integral += days_integrals[days[i]];
            }
        }
        return integral;
    }
</script>

<script>
    var toast = new auiToast({
    })

    $("#convert-btn").click(function () {
        $(".light-hint").fadeIn("slow");
        setTimeout("$('.light-hint').fadeOut('slow')", 2000);
    });

    function exchangeCoupon(couponid, integral) {
        var myInte =  $("#integralSum").text();
        if(integral > myInte) {
            toast.fail({
                title:"您当前积分不足以兑换该优惠券",
                duration:2000
            });
        }else {
            $w.httpRequest({
                url : "${ctx}/we/home/exchangeCoupon",
                data : {"couponid" : couponid},
                ok : function (ret) {
                    var myIntegral = ret.Data.myIntegral;
                    var inteSum = myIntegral.integral + myIntegral.ly_integral;
                    $("#integralSum").text(inteSum);
                    toast.success({
                        title:ret.Data.status,
                        duration:2000
                    });
                }
            });
        }
    }

</script>
</body>
</html>
